package com.dev.security.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.dev.security.login.service.LoginManager;
import com.dev.security.login.service.LoginSSOFilter;
import com.dev.security.login.service.UserService;
import com.dev.security.login.service.ValidateFilter;
import com.dev.security.login.service.filters.ForbidFilter;
import com.dev.security.login.service.filters.NoLoginFilter;
import com.dev.security.login.service.filters.RootFilter;
import com.dev.security.login.service.filters.StatusFilter;

/**
 * @author 潜行的青衣
 * @see
 */
@Configuration
public class LoginConfig {

	@Autowired
	private UserService userService;
	
	@Autowired(required = false)
	private LoginSSOFilter loginSSOFilter;
	
	@Autowired
	private Environment env;
	
	@Bean
	public LoginManager loginManager() {
		LoginManager login = new LoginManager();
		login.setUserService(userService);
		login.setPasswordEncoder(passwordEncoder());
		login.setLoginSSOFilter(loginSSOFilter);
		login.setCookie(true);
		login.setCookieEncryptKey(env.getProperty("cookie.encrypt.key"));
		List<ValidateFilter> validateFilters = new ArrayList<ValidateFilter>();
		validateFilters.add(new NoLoginFilter());
		validateFilters.add(new StatusFilter());
		validateFilters.add(new RootFilter());
		ForbidFilter forbidFilter = new ForbidFilter(this.userService);
		validateFilters.add(forbidFilter);
		login.setValidateFilters(validateFilters);
		return login;
	}
	
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
}
